package com.gxa.pingan.controller;

import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.pojo.City;
import com.gxa.pingan.service.CityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * @author 18380
 */
@Controller
@ResponseBody
@RequestMapping("/city")
@Api(value = "城市操作的控制器", tags = "城市的操作的接口")
public class CityController {

    private final static Integer SIZE = 10;

    @Autowired
    CityService cityService = null;

    @PostMapping("/add")
    @ApiOperation("添加城市的方法")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "city", value = "请传city的JSON字符串", type = "JSON")})
    public Map add(@RequestBody City city) throws Exception {
        cityService.add(city);
        JsonResult jsonResult = new JsonResult();
        int size = cityService.size();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        jsonResult.put("total", size);
        return jsonResult.getDataMap();
    }

    @PostMapping("/findAll")
    @ApiOperation("查询全部的方法")
    public Map findAll() throws Exception {
        List<City> allOrByPage = cityService.findAll();
        int size = cityService.size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.put("total", size);
        jsonResult.setData(allOrByPage);
        return jsonResult.getDataMap();
    }

    @PostMapping("/findByPage")
    @ApiOperation("这个是分页查询")
    public Map findByPage(Integer page) throws Exception {
        List<City> byPage = cityService.findByPage(page, SIZE);
        int size = cityService.size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(byPage);
        jsonResult.put("count", size);
        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    @ApiImplicitParam(name = "id", value = "id", paramType = "query", required = true)
    public Map<String, Object> delete(Long id) throws Exception {
        cityService.deleteCity(id);
        int size = cityService.size();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");
        jsonResult.put("total", size);
        return jsonResult.getDataMap();
    }

}
